package _thread_lock._thread;

import java.util.concurrent.TimeUnit;

/**
 * @Author: huangya
 * @Date: 2020/09/25
 * @Description:
 */
public class ThreadIsInterrupted {

    private static int flag = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run(){
                //while (flag==0) {
                //    System.out.println(0);
                //}
                try {
                    System.out.println(1);
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(2);
                } catch (InterruptedException e) {
                    System.out.println("Thread.interrupted:" + Thread.interrupted());
                    System.out.println(3);
                    try {
                        TimeUnit.SECONDS.sleep(5);
                        System.out.println(7);
                    } catch (InterruptedException interruptedException) {
                        System.out.println(8);
                    }
                }
            }
        };
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("before:" + thread.isInterrupted());
        thread.interrupt();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("after:" + thread.isInterrupted());
        flag=1;
        System.out.println("after2:" + thread.isInterrupted());

        System.out.println("after3:" + thread.isInterrupted());
    }
}
